package com.example.lhh.user.service.impl;

import com.alibaba.fastjson2.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.example.lhh.user.entity.LhhPermission;
import com.example.lhh.user.entity.LhhUser;
import com.example.lhh.user.mapper.LhhUserMapper;
import com.example.lhh.user.service.ILhhPermissionService;
import com.example.lhh.user.service.ILhhUserService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.lhh.util.LhhSaltUtil;
import com.example.lhh.util.LhhSha256Util;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;


/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author luoHongHu
 * @since 2025-03-25
 */
@Service
public class LhhUserServiceImpl extends ServiceImpl<LhhUserMapper, LhhUser> implements ILhhUserService {

    @Resource
    private LhhUserMapper lhhUserMapper;

    @Resource
    private ILhhPermissionService iLhhPermissionService;

    /**
     * 注册
     * @param userNo 用户名
     * @param password 密码
     */
    @Override
    public JSONObject register(String userNo, String password) {
        JSONObject jsonObject = new JSONObject();
        //获取盐
        String saltValue = LhhSaltUtil.randomSalt();
        //加密后的hash密码
        String passwordHash = LhhSha256Util.createPassword(saltValue+password);
        try {
            LhhUser user = new LhhUser();
            user.setUserName(userNo);
            user.setUserNo(userNo);
            user.setPassWord(passwordHash);
            user.setSalt(saltValue);

            QueryWrapper queryWrapper = new QueryWrapper();
            queryWrapper.eq("user_no", userNo);
            LhhUser lhhUser = lhhUserMapper.selectOne(queryWrapper);
            if(lhhUser != null) {
                jsonObject.put("success", false);
                jsonObject.put("msg", "不能重复注册同一个用户");
                return jsonObject;
            }
            //插入
            lhhUserMapper.insert(user);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
        jsonObject.put("success", true);
        jsonObject.put("msg", "不能重复注册同一个用户");
        return jsonObject;
    }

    @Override
    public LhhUser getLhhUserByUserNo(String userNo) {
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("user_no", userNo);
        return lhhUserMapper.selectOne(queryWrapper);
    }

    /**
     * 通过用户名获取该用户菜单
     * @param userNo
     * @return
     */
    @Override
    public LhhUser getRoleByUserName(String userNo) {
        return lhhUserMapper.getRoleByUserName(userNo);
    }

    /**
     * 获取用户菜单
     * @return
     */
    @Override
    public List<Map<String,Object>> getUserMenu(String userNo) {
        List<LhhPermission> parentMentList = iLhhPermissionService.getParentMenuByUserNo(userNo);
        List<Map<String,Object>> menuList = new ArrayList<>();
        if(parentMentList.size() > 0){
            for(LhhPermission lhhPermission : parentMentList){
                JSONObject jsonObject = new JSONObject();
                jsonObject.put("parentMenu",lhhPermission);
                //获取该父级菜单下面的子菜单
                String perId = lhhPermission.getPerId();
                List<LhhPermission> LhhPermissionList = iLhhPermissionService.getSubeMnuByParentCode(perId);
                jsonObject.put("subMenu",LhhPermissionList);
                menuList.add(jsonObject);
            }
        }
        return menuList;
    }



}
